package com.wfs.aoawlms.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wfs.aoawlms.mapper.LogMapper;
import com.wfs.aoawlms.pojo.Log;
import com.wfs.aoawlms.pojo.PageBean;
import com.wfs.aoawlms.service.LogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Slf4j
@Service
public class LogServiceImpl implements LogService {
    @Autowired
    private LogMapper logMapper;
    @Override
    public void logtest() {

    }

    //@Override
   // public List<Log> list(String name,String region_name) {
    //    List<Log> logs = logMapper.list(name,region_name);
    //    return logs;
    //}

    @Override
    public int getRegionId(String region_name) {
         int region_id = logMapper.getRegionId(region_name);
         return  region_id;
    }

    @Override
    public Log addLog(Log log,int region_id) {
        LocalDateTime updateTime = LocalDateTime.now();
        log.setUpdatetime(updateTime);
        Log logWithUpdatetime = log;
        logMapper.addLog(logWithUpdatetime,region_id);
        return logWithUpdatetime;
    }

    @Override
    public void deleteLog(Integer logId) {
        logMapper.deleteLog(logId);
    }


    @Override
    public Log editLog(Log log, int region_id) {
        LocalDateTime updateTime = LocalDateTime.now();
        log.setUpdatetime(updateTime);
        Log logWithUpdatetime = log;
        logMapper.editLog(log,region_id);
        return logWithUpdatetime;
    }

    @Override
    public int getLogId(String name, String description, String solution, Boolean isSolved) {
        int logId = logMapper.getLogId(name,description,solution,isSolved);
        log.info("logId:{}",logId);
        return logId;
    }

    @Override
    public PageBean page(Integer page, Integer pageSize,String problem_type,String name) {
        // 1设置分页参数
        PageHelper.startPage(page,pageSize);
        // 2执行分页查询
        List<Log> logList = logMapper.list(problem_type,name);
        log.info("service层：",String.valueOf(logList));
        Page<Log> p =(Page<Log>) logList;
        // 3获取分页结果
        PageBean pageBean = new PageBean(p.getTotal(),p.getResult());
        // 封装PageBean
        return pageBean;
    }

    @Override
    public void upload(MultipartFile file, int logId) throws IOException {
        //本地上传的目的：将前端上传的文件转存到本地中，是因为前端上传的文件，后端存的是临时文件，方法运行完毕文件也会消失
        //获取上传文件的名字
        String filename=file.getOriginalFilename();
        //随机的文件名
        UUID uuid = UUID.randomUUID();
        //把随机文件名和扩展名拼接
        String name=filename.substring(filename.lastIndexOf("."));
        String newFileName = uuid+name;
        //拼接本机相对文件地址
        String imageName = "http://192.168.43.7:1013/images/"+newFileName;
        //储存地址到数据表
        logMapper.upload(imageName,logId);
        //把临时文件存储到本地硬盘中
        file.transferTo(new File("D:\\code\\AOA工作日志管理系统\\AOAwlms\\src\\main\\resources\\static\\images",newFileName));

    }


}
